@echo off
REM list the files under current directory AND sub-directories
REM Usage: flat [-p] [file types]
REM Example: flat -p *txt

REM set initial variables
setlocal ENABLEDELAYEDEXPANSION
set total=0
set pflag=0
set ftype=""

REM pre-process on options
if /i "%~1" == "-h" goto HELPMSG
if "%~1" == "-?" goto HELPMSG

if /i "%~1" == "-p" (
    set pflag=1
    set ftype="%~2"
) else (
    set ftype="%~1"
)

REM implement with for and dir commands
for /f "tokens=*" %%a in ('dir /s /b /a:-d !ftype!') do @if NOT ERRORLEVEL 1 (
    if !pflag! == 1 echo %%a
    if !pflag! == 0 echo %%~nxa
    set /a total+=1
) else (
    goto ABORT
)
goto END

REM help message
:HELPMSG
echo Usage: flat [options] [file types]
echo options:
echo -p        include file path
echo -h or -?  help message
goto EOF

REM post-display
:ABORT
echo  Error happened~~~
goto EOF
:END
echo *** Total !total! files ***
goto EOF
:EOF
